﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//using MvcReservas.Models;
using MvcConfirmarReserva.Models;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;
using System.Collections;
using System.Text;

namespace MvcReservas.Controllers
{
    public class ReservaController : Controller
    {
        //
        // GET: /Reservas
        // Carga la lista de reservas
        public ActionResult Index()
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:23374/Reservas.svc/ReservasPendientes");
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string reservaJson = reader.ReadToEnd();
            JavaScriptSerializer js = new JavaScriptSerializer();
            List<Reserva> reservasObtenidas = js.Deserialize<List<Reserva>>(reservaJson);
            
            //solo deja las reservas por confirmar
            //foreach (Reserva reservaAConfirmar in reservasObtenidas)
          

            return View(reservasObtenidas);
        }
        
        public ActionResult Accept(int id)
        {
            //Colas
            try
            {
                //obtengo el objeto
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:23374/Reservas.svc/Reservas/" + id.ToString());
                req.Method = "GET";
                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                StreamReader reader = new StreamReader(res.GetResponseStream());
                string reservaJson = reader.ReadToEnd();
                JavaScriptSerializer js = new JavaScriptSerializer();
                Reserva reservaAConfirmar = js.Deserialize<Reserva>(reservaJson);

                //cambio el estado
                reservaAConfirmar.estado = "1";

               
                js = new JavaScriptSerializer();
                string postdata = js.Serialize(reservaAConfirmar);
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req1 = (HttpWebRequest)WebRequest.Create("http://localhost:23374/Reservas.svc/ReservasPendientes");
                req1.Method = "PUT";
                req1.ContentLength = data.Length;
                req1.ContentType = "application/json";
                var reqStream = req1.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                


               
           

            //Listado
                req = (HttpWebRequest)WebRequest.Create("http://localhost:23374/Reservas.svc/Reservadas");
            req.Method = "GET";
            res = (HttpWebResponse)req.GetResponse();
            reader = new StreamReader(res.GetResponseStream());
            reservaJson = reader.ReadToEnd();
            js = new JavaScriptSerializer();
            List<Reserva> reservasObtenidas = js.Deserialize<List<Reserva>>(reservaJson);

            //solo deja las reservas por confirmar
            //foreach (Reserva reservaAConfirmar in reservasObtenidas)
           

                return View("Index",reservasObtenidas);

            }
            catch (Exception e)
            {
                //ModelState.AddModelError(String.Empty, "Error: " + e.Message);

                //List<Reserva> reservasObtenidas = js.Deserialize<List<Reserva>>(reservaJson);

                ////solo deja las reservas por confirmar
                ////foreach (Reserva reservaAConfirmar in reservasObtenidas)
                //for (int i = 0; i < reservasObtenidas.Count(); i++)
                //{
                //    Reserva reservaAConfirmar2 = (Reserva)reservasObtenidas[i];
                //    if (reservaAConfirmar2.estado == "0")
                //        reservasObtenidas.Remove(reservaAConfirmar2);

                //}

                return View("Index");
            }
        }

        public ActionResult Reject(int id)
        {
            //Colas
            try
            {
                //obtengo el objeto
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:23374/Reservas.svc/Reservas/" + id.ToString());
                req.Method = "GET";
                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                StreamReader reader = new StreamReader(res.GetResponseStream());
                string reservaJson = reader.ReadToEnd();
                JavaScriptSerializer js = new JavaScriptSerializer();
                Reserva reservaAConfirmar = js.Deserialize<Reserva>(reservaJson);

                //cambio el estado
                reservaAConfirmar.estado = "2";


                js = new JavaScriptSerializer();
                string postdata = js.Serialize(reservaAConfirmar);
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req1 = (HttpWebRequest)WebRequest.Create("http://localhost:23374/Reservas.svc/ReservasPendientes");
                req1.Method = "PUT";
                req1.ContentLength = data.Length;
                req1.ContentType = "application/json";
                var reqStream = req1.GetRequestStream();
                reqStream.Write(data, 0, data.Length);

                //Listado
                req = (HttpWebRequest)WebRequest.Create("http://localhost:23374/Reservas.svc/Reservadas");
                req.Method = "GET";
                res = (HttpWebResponse)req.GetResponse();
                reader = new StreamReader(res.GetResponseStream());
                reservaJson = reader.ReadToEnd();
                js = new JavaScriptSerializer();
                List<Reserva> reservasObtenidas = js.Deserialize<List<Reserva>>(reservaJson);

                //solo deja las reservas por confirmar
                //foreach (Reserva reservaAConfirmar in reservasObtenidas)


                return View("Index", reservasObtenidas);

            }
            catch (Exception e)
            {
                ModelState.AddModelError(String.Empty, "Error: " + e.Message);
                return View("Index");
            }
        }

       


    }
}
